package com.hspedu.mhl.dao;

/**
 * @author lianghao
 * @create 2022-07-11 17:29
 */


import com.hspedu.mhl.utils.JDBCUtilsBYDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * dao的父类Basicdao
 *
 * @param <T>
 */
public class BasicDAO<T>{//可以指定具体类型

    private Class<T> clazz = null;

    {
        //获取当前BaseDAO的子类继承的父类中的泛型
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        ParameterizedType paramType = (ParameterizedType) genericSuperclass;

        Type[] typeArguments = paramType.getActualTypeArguments();//获取了父类的泛型参数
        clazz = (Class<T>) typeArguments[0];//泛型的第一个参数

    }

    private  QueryRunner qr =  new QueryRunner();
    //开发dml可以针对任意的表
    public int update(String sql,Object... parameters){
        Connection connection = null;

        try {
            connection = JDBCUtilsBYDruid.getConnection();
            int update = qr.update(connection, sql, parameters);
            return update;
        } catch (SQLException e) {//将编译异常转换成运行异常
            throw new  RuntimeException(e);
        } finally {
            JDBCUtilsBYDruid.close(null,null,connection);
        }
    }

    //返回多个查询的对象（即查询结果返回多行）
    /**
     *
     * @param sql:输入的sql语句
     * @param parameters:给sql语句当中的 ？？ 进行赋值
     * @return
     */
    public List<T> queryMulti(String sql,Object... parameters) {

        Connection connection = null;

        try {
            connection = JDBCUtilsBYDruid.getConnection();
            List<T> list = qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtilsBYDruid.close(null, null, connection);
        }

        return null;
    }

    //查询单行的结果

    public T querySingle(String sql,Object... parameters){

        Connection connection = null;

        try {
            connection = JDBCUtilsBYDruid.getConnection();
            T query = qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

        }
        return null;
    }

    //查询单个数据
    public Object queryScalar(String sql,Object... paramenters){
        Connection connection = null;

        try {
            connection = JDBCUtilsBYDruid.getConnection();
            Object query = qr.query(connection, sql, new ScalarHandler(), paramenters);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtilsBYDruid.close(null,null,connection);
        }

        return null;
    }


}


